//
//  GMGuidePageView.m
//  Teecloud
//
//  Created by 小熊 on 2018/5/24.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMGuidePageView.h"
#import "GMOrgCell.h"
#import "GMOrgModel.h"
#define kScreen_height  [[UIScreen mainScreen] bounds].size.height
#define kScreen_width   [[UIScreen mainScreen] bounds].size.width
@interface GMGuidePageView ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *searchTextTF;
@property (weak, nonatomic) IBOutlet UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *searchKey;
@end

@implementation GMGuidePageView
-(void)awakeFromNib{
    [super awakeFromNib];
    self.searchKey = @"";
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    self.searchTextTF.delegate = self;
    [self.tabView registerNib:[UINib nibWithNibName:@"GMOrgCell" bundle:nil] forCellReuseIdentifier:@"GMOrgCell"];
    [self getData];
}
-(void)getData{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.searchKey forKey:@"key"];
    [MBProgressHUD showHUDAddedTo:self animated:YES];
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"do-api.ashx?$m=GetCompanyList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyNetworkElseCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSString *temStr =[responseObject objectForKey:RESULT_NAME_Message];
                if (kIsEmptyString(temStr)) {
                    //如果msg为空，就弹出运营列表
                    NSArray *arr = [GMOrgModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                    [self.dataArr removeAllObjects];
                    [self.dataArr addObjectsFromArray:arr];
                    [self.tabView reloadData];
                }else{
                    /**存储选中运营机构信息*/
                    [XGMArchiverTools cache:temStr ForKey:BUSIORGID];
                    [self hideGuideView];
                }
              
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"请求出错" toView:self];
        }
    }];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMOrgCell *cell =[tableView dequeueReusableCellWithIdentifier:@"GMOrgCell" forIndexPath:indexPath];
    GMOrgModel *model = self.dataArr[indexPath.row];
    cell.Name.text = model.Name;
    cell.OrgName.text = [NSString stringWithFormat:@"(%@)",model.OrgName];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GMOrgModel *model = self.dataArr[indexPath.row];
    /**存储选中运营机构信息*/
    [XGMArchiverTools cache:model.BusiOrgId ForKey:BUSIORGID];
    [XGMArchiverTools cache:model.Name ForKey:BUSName];
    [XGMArchiverTools cache:model.OrgName ForKey:BUSOrgName];
    [self hideGuideView];
}
- (void)hideGuideView {
    //动画隐藏
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        //延迟0.5秒移除
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromSuperview];
        });
    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.searchKey = textField.text;
    [self getData];
}
@end
